//
//  WJListMenuViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/8/8.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "WJListMenuViewController.h"
#import "StarView.h"
#import "MineViewTableViewCell.h"

#import "LoginViewController.h"
#import "MineOrderViewController.h"
#import "OverInfomationViewController.h"
#import "ReleaseServerViewController.h"
#import "SendedJobListViewController.h"
#import "MyAssetsViewController.h"
#import "MessageAndTalkAboutViewController.h"
#import "MineManagerHomeViewController.h"
#import "SetSystemInfomationViewController.h"
#import "DetailMakeSureViewController.h"
#import "SendServersListViewController.h"

@interface WJListMenuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIButton *dreamBtn;

@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *userImageView; // y用户头像
@property (nonatomic, strong) UILabel *userNameLabel; // 用户名
@property (nonatomic, strong) UILabel *userLeveLabel; // 用户等级
@property (nonatomic, strong) StarView *starView; // 小星星
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIButton *havePrizeBtn; // 推荐有奖
@property (nonatomic, strong) UIButton *mineHomeBtn; // 我的管家
@property (nonatomic, strong) UIImageView *showHideImg; // 倒三角

@end

@implementation WJListMenuViewController
// 获取用户信息
- (void)getInfoMations {
    if ([[UserModel sharedInstanced].userId isEqualToString:@""] || ![UserModel sharedInstanced].userId) {
        return;
    }
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=userinfo" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"error_code"]];
        if ([str isEqualToString:@"0"]) {
            NSDictionary *userDataDic = responseObject[@"data"];
            [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userDataDic[@"avatar"]]];
            self.userNameLabel.text = userDataDic[@"username"]; // 用户名
            int startsNum = [userDataDic[@"startnum"] intValue];
            [_starView showMarkWithStar:startsNum];
            self.userLeveLabel.text = [NSString stringWithFormat:@"%d.0",startsNum];
            if (userDataDic[@"avatar"] != NULL || userDataDic[@"avatar"] != nil) {
                [UserModel sharedInstanced].headImg = userDataDic[@"avatar"]; // 头像
            }
            if (userDataDic[@"username"] != NULL || userDataDic[@"username"] != nil) {
                [UserModel sharedInstanced].nameNick = userDataDic[@"username"]; // 昵称
            }
        }else{
            //            ShowMessage(@"操作失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //        [self showError:error];
    }];
}
#pragma mark - event
// 头像点击
- (void)imagesViewTap:(UITapGestureRecognizer *)tapgesture
{
    if (![self isLoginIn]) {
        return;
    };
    NSLog(@"用户头像点击跳转完善信息");
    OverInfomationViewController * VC = [[OverInfomationViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];

}
// 右面灰色按钮
- (void)cancelBtnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)havePrizeBtnAction:(UIButton *)btn
{
    if ([btn isEqual:self.havePrizeBtn]) {
        NSLog(@"推荐有奖");
        [UIViewController showShareType:@"1"]; // 具体参数 请查看方法 .h文件
    }
    if ([btn isEqual:self.mineHomeBtn]) {
        NSLog(@"我的管家");
        MineManagerHomeViewController * VC = [[MineManagerHomeViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if (![self isLoginIn]) {
        return;
    };
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [self setUpView];
    [self getInfoMations];
}
- (void)setUpView
{
    // 右面灰色按钮
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [backButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    backButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:backButton];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240 / WIDTH_6S_SCALE, SCREEN_HEIGHT)];
    bgView.backgroundColor = getColor(whiteColor);
    self.bgView = bgView;
    [self.view addSubview:bgView];
    [bgView addSubview:self.mainTableView];
    
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MineViewTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.dataDictionary = self.dataArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![self isLoginIn]) {
        return;
    };
    if (indexPath.row == 0) {
        NSLog(@"我的订单");
        MineOrderViewController * VC = [[MineOrderViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.row == 1) {
        NSLog(@"完善信息");
        OverInfomationViewController * VC = [[OverInfomationViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.row == 2) {
        NSLog(@"发布服务");
        SendServersListViewController *VC = [[SendServersListViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.row == 3) {
        NSLog(@"发布岗位");
        SendedJobListViewController *VC = [[SendedJobListViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.row == 4) {
        NSLog(@"我的资产");
        MyAssetsViewController *VC = [[MyAssetsViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.row == 5) {
        NSLog(@"系统设置");
        SetSystemInfomationViewController *VC = [[SetSystemInfomationViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:self.bgView.frame style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(whiteColor);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[MineViewTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MineViewTableViewCell class])];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140/HEIGHT_6S_SCALE)];
        _headerView.backgroundColor = [UIColor clearColor];
        
        [_headerView addSubview:self.userImageView];
        [_headerView addSubview:self.userNameLabel];
        [_headerView addSubview:self.starView];
        [_headerView addSubview:self.userLeveLabel];
        UIView * otherLogLineView = [[UIView alloc] init];
        otherLogLineView.backgroundColor = getColor(@"e9e9e9");
        [_headerView addSubview:otherLogLineView];
        [otherLogLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headerView.mas_centerX);
            make.bottom.equalTo(_headerView.mas_bottom).offset(-10 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(1);
        }];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 140/HEIGHT_6S_SCALE - 6 * 45)];
        
        [_footerView addSubview:self.havePrizeBtn];
        UILabel *jpLabel = [[UILabel alloc]init];
        jpLabel.text = @"推荐有奖";
        jpLabel.textAlignment = NSTextAlignmentCenter;
        jpLabel.textColor =getColor(textColor);
        jpLabel.font =DEF_FontSize_14;
        [_footerView addSubview:jpLabel];
        
        [_footerView addSubview:self.mineHomeBtn];
        UILabel *myHomeLabel = [[UILabel alloc]init];
        myHomeLabel.text = @"我的管家";
        myHomeLabel.textAlignment = NSTextAlignmentCenter;
        myHomeLabel.textColor =getColor(textColor);
        myHomeLabel.font =DEF_FontSize_14;
        [_footerView addSubview:myHomeLabel];
        [_footerView addSubview:self.showHideImg];
        
        __weak typeof(self)weakself = self;
        [self.havePrizeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_footerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.bottom.equalTo(_footerView.mas_bottom).offset(-60 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(100 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(80 / HEIGHT_6S_SCALE);
        }];
        [jpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakself.havePrizeBtn.mas_centerX);
            make.top.equalTo(weakself.havePrizeBtn.mas_bottom).offset(-15 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(100 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        [self.mineHomeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakself.footerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.centerY.equalTo(weakself.havePrizeBtn.mas_centerY);
            make.width.mas_equalTo(100 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(80 / HEIGHT_6S_SCALE);
        }];
        [myHomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakself.mineHomeBtn.mas_centerX);
            make.top.equalTo(weakself.mineHomeBtn.mas_bottom).offset(-15 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(100 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        [self.showHideImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_footerView.mas_centerX);
            make.bottom.equalTo(_havePrizeBtn.mas_top).offset(- 5);
        }];
    }
    return _footerView;
}
- (UIImageView *)userImageView
{
    if (!_userImageView) {
        _userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20 / WIDTH_6S_SCALE, self.headerView.centerY - 57 * 0.5, 58 / WIDTH_6S_SCALE, 58 / HEIGHT_6S_SCALE)];
        _userImageView.backgroundColor = [UIColor clearColor];
        _userImageView.userInteractionEnabled = YES;
        _userImageView.image = [UIImage imageNamed:@"list_headerImg"];
        UITapGestureRecognizer * imagesViewTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagesViewTap:)];
        [_userImageView addGestureRecognizer:imagesViewTapGestureRecognizer];
        _userImageView.layer.cornerRadius = 29 / HEIGHT_6S_SCALE;
        _userImageView.layer.masksToBounds = YES;
    }
    return _userImageView;
}
- (UILabel *)userNameLabel
{
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.userImageView.frame) + 20 / WIDTH_6S_SCALE, self.userImageView.centerY - 20, 128 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
        _userNameLabel.backgroundColor = [UIColor clearColor];
        _userNameLabel.textColor = getColor(textColor);
        _userNameLabel.font = DEF_FontSize_18;
        _userNameLabel.text = @"昵称未设置";
    }
    return _userNameLabel;
}

- (StarView *)starView{
    if(!_starView){
        
        _starView = [[StarView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.userImageView.frame) + 20 / WIDTH_6S_SCALE, self.userImageView.centerY + 10, 60/WIDTH_5S_SCALE, 15/WIDTH_5S_SCALE)];
        [_starView showMarkWithStar:0];
        _starView.userInteractionEnabled = NO;
    }
    return _starView;
}
- (UILabel *)userLeveLabel
{
    if (!_userLeveLabel) {
        _userLeveLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.starView.frame) + 10 / WIDTH_6S_SCALE, self.userImageView.centerY + 10, 30 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
        _userLeveLabel.backgroundColor = [UIColor clearColor];
        _userLeveLabel.textColor = getColor(textColor);
        _userLeveLabel.font = DEF_FontSize_14;
        _userLeveLabel.text = @"0";
    }
    return _userLeveLabel;
}
- (UIButton *)havePrizeBtn{
    if (!_havePrizeBtn) {
        _havePrizeBtn = [[UIButton alloc] init];
        _havePrizeBtn.titleLabel.font = DEF_FontSize_12;
        [_havePrizeBtn setImage:[UIImage imageNamed:@"lefebar_ico.7"] forState:UIControlStateNormal];
        [_havePrizeBtn addTarget:self action:@selector(havePrizeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }return _havePrizeBtn;
}
- (UIButton *)mineHomeBtn
{
    if (!_mineHomeBtn) {
        _mineHomeBtn = [[UIButton alloc]init];
        _mineHomeBtn.titleLabel.font = DEF_FontSize_12;
        [_mineHomeBtn setImage:[UIImage imageNamed:@"lefebar_ico.8"] forState:UIControlStateNormal];
        [_mineHomeBtn addTarget:self action:@selector(havePrizeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mineHomeBtn;
}
- (UIImageView *)showHideImg
{
    if (!_showHideImg) {
        _showHideImg = [[UIImageView alloc]init];
        _showHideImg.backgroundColor = [UIColor clearColor];
        _showHideImg.userInteractionEnabled = YES;
        _showHideImg.image = [UIImage imageNamed:@"lefebar_sj"];
        UITapGestureRecognizer * imagesViewTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showHideImgTap:)];
        [_showHideImg addGestureRecognizer:imagesViewTapGestureRecognizer];
    }
    return _showHideImg;
}
- (void)showHideImgTap:(UITapGestureRecognizer *)tapgesture
{
    
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = @[@{@"imageName":@"lefebar_ico.1",@"titleNmae":@"我的订单"},
                       @{@"imageName":@"lefebar_ico.2",@"titleNmae":@"完善信息"},
                       @{@"imageName":@"lefebar_ico.3",@"titleNmae":@"发布服务"},
                       @{@"imageName":@"lefebar_ico.4",@"titleNmae":@"发布岗位"},
                       @{@"imageName":@"lefebar_ico.5",@"titleNmae":@"我的资产"},
                       @{@"imageName":@"lefebar_ico.6",@"titleNmae":@"系统设置"},].mutableCopy;
    }
    return _dataArray;
}

@end
